/**
 * Created by YY on 16/9/26.
 */
angular.module('ProductDisplay')
    .factory('ProductDisplay.Service', ['$q', 'Http',
        function ($q, Http) {
            var o = {};
            var departments, workLocation = [];
            o.queryDepartment = function (department) {
                var deferred = $q.defer();
                Http.post({
                    url: '/api/barcode/queryDepartment',
                    data: {
                        parentDepartment: department
                    }
                }).then(function (records) {
                    deferred.resolve(records);
                }, function (err) {
                    deferred.reject(err);
                });
                return deferred.promise;
            };

            o.queryWorkLocation = function (departments) {
                var deferred = $q.defer();
                Http.post({
                    url: '/api/barcode/queryWorkLocationByDepartment',
                    data: {
                        departments: departments
                    }
                }).then(function (records) {
                    deferred.resolve(records);
                }, function (err) {
                    deferred.reject(err);
                });
                return deferred.promise;
            };

            o.countProductView = function (workLocations, flg) {
                var deferred = $q.defer();
                Http.post({
                    url: '/api/barcode/countProductView',
                    data: {
                        workLocations: workLocations,
                        flg: flg,
                        startTime: '00:00:00',
                        endTime: '23:59:59'
                    }
                }).then(function (records) {
                    deferred.resolve(records);
                }, function (err) {
                    deferred.reject(err);
                });
                return deferred.promise;
            };

            o.setDepartment = function (obj) {
                departments = obj;
            };

            o.getDepartment = function () {
                return departments;
            };

            o.setWorkLocation = function (arr) {
                workLocation = arr;
            };

            o.getWorkLocation = function () {
                return workLocation;
            };
            o.resetDefault = function () {
                departments = undefined;
                workLocation = [];
            };
            return o;
        }]);